<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use App\Models\Car\CarCost;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\VehicleAllocation;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\Service;
use Modules\Car\Entities\Maintenance;

//车辆分摊控制器
class VehicleAllocationController extends Controller
{
    use ResponseData,PayTrait;
    public function list(VehicleAllocation $vehicle,Request $request){
        $size = $request->input('size',10);
        $model = $vehicle->query()->whereNull('deleted_at');
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        if($request->filled('plate_number')){
            $model = $model->where('car_names','like','%'.$request->plate_number.'%');
        }
        if($request->filled('nick_name')){
            $model = $model->where('user_names','like','%'.$request->nick_name.'%');
        }
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        $data = $model->orderBy('id','desc')->paginate($size);
        return $this->success($data);
    }
    public function store(VehicleAllocation $vehicle,Request $request){
        $data = $request->all();
        $data['order_sn'] = $this->getSn();
        $model = $vehicle->query()->create($data);
        if($request->status==1){
            $array = explode(",", $request->car_ids);
            foreach ($array as $key=>$value){
                $trimid = trim($value, '_');
                CarCost::query()->where('car_id', $trimid)->update(['share_status'=>2]);
                RecognizeDrivingLicense::query()->where('id', $trimid)->update(['share_status'=>2]);
            }
        }
        return $this->success($model->id);
    }
    //设置订单号
    private function getSn(){
        $dar = VehicleAllocation::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($dar){
            $num = $dar->id+1;
        }else{
            $num = 1;
        }
        return date('Ymd',time()).$num;
    }
    public function edit(VehicleAllocation $vehicle,Request $request){
        $data = $vehicle->find($request->id);
        return $this->success($data);
    }
    public function update(VehicleAllocation $vehicle,Request $request){
        $data = $vehicle->find($request->id);
        $data->update($request->all());
        if($request->status==1){
            $array = explode(",", $request->car_ids);
            foreach ($array as $key=>$value){
                $trimid = trim($value, '_');
                CarCost::query()->where('car_id', $trimid)->update(['share_status'=>2]);
                RecognizeDrivingLicense::query()->where('id', $trimid)->update(['share_status'=>2]);
            }
        }
        return $this->success('修改成功');
    }
    public function destory(VehicleAllocation $vehicle,Request $request){
        $data = $vehicle->find($request->id);
        $array = explode(",", $data->car_ids);
        foreach ($array as $key=>$value){
            $trimid = trim($value, '_');
            CarCost::query()->where('car_id', $trimid)->update(['share_status'=>1]);
            RecognizeDrivingLicense::query()->where('id', $trimid)->update(['share_status'=>1]);
        }
        $data->deleted_at = now();
        $data->save();
        return $this->success('删除成功');
    }
    // 收支单详情
    public function detail(VehicleAllocation $vehicle,Request $request){
        // 车牌号数组
        $plate_numbers = explode(',', $request->plate_numbers);
        // 车辆订单-已入账的
        $orderlist = CarOrder::query()->whereNotNull('ok_reconciliation_time')->whereIn('plate_number',$plate_numbers);
        // 车辆维修单-已完成的
        $wxlist = Service::query()->where('status',4)->whereIn('plate_number',$plate_numbers);
        // 车辆保养单-已完成的
        $bylist = Maintenance::query()->whereNotNull('end')->whereIn('plate_number',$plate_numbers);
        // 车辆处置单
        $czlist = RecognizeDrivingLicense::onlyTrashed()->whereNotNull('last_mark')->whereIn('plate_number',$plate_numbers);
        if($request->filled('start')&&$request->filled('end')){
            $orderlist = $orderlist->whereBetween('ok_reconciliation_time',[$request->start,$request->end])->get();
            $wxlist = $wxlist->whereBetween('created_at',[$request->start,$request->end])->get();
            $bylist = $bylist->whereBetween('created_at',[$request->start,$request->end])->get();
            $czlist = $czlist->whereBetween('deleted_at',[$request->start,$request->end])->get();
        }else{
            $orderlist = $orderlist->get();
            $wxlist = $wxlist->get();
            $bylist = $bylist->get();
            $czlist = $czlist->get();
        }
        return $this->success(compact('orderlist','wxlist','bylist','czlist'));
    }
}
